import init from '@feidao/web/framework/browser/d/component';
import { id, spaceid, systemid, refsystemid } from './config';
import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import update_pwd from '@feidao/web/atom/browser/msg/update-pwd';

import storage_clear from '@feidao/web/atom/browser/storage/clear';
import url_redirect from '@feidao/web/atom/browser/url/direct-to'
import user_loginout from '@feidao/web/atom/browser/msg/user-logout';
import fire from '@feidao/web/atom/browser/msg/fire';

import { send_msg as url, get_user_info } from './config';

import whoami from '@feidao/web/atom/browser/msg/whoami';


import hide from '../../atom/hide';
import show from '../../atom/show';
const actions = {
	cmpt_show(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		// 将已经输入的内容清空
		const inputs = fd.data.node.querySelectorAll('input[type="password"]');
		Array.from(inputs).forEach((i: HTMLInputElement) => {
			i.value = '';
		});
		// 将错误信息提示隐藏
		const n = fd.data.node.querySelector('[data-feidao-id="error_div"]') as HTMLElement;
		hide(n);
		show(fd.data.node);
		fd.data.node.querySelector('[data-feidao-zjc="06"]').classList.remove('display-n')
	},
	close_cmpt(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		hide(fd.data.node);
	},

	async upd_pwd(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		// 获取输入框的内容
		const current_pwd = fd.data.node.querySelector('[data-feidao-id="current_pwd"]') as HTMLInputElement;
		if (!current_pwd.value) {
			fire('zjc-000002', 'ec-001', 'fail', '请输入当前密码');
			return;
		}
		const new_pwd = fd.data.node.querySelector('[data-feidao-id="new_pwd"]') as HTMLInputElement;
		if (!new_pwd.value) {
			fire('zjc-000002', 'ec-001', 'fail', '请输入新密码');
			return;
		}

		const sure_pwd = fd.data.node.querySelector('[data-feidao-id="sure_pwd"]') as HTMLInputElement;
		if (!sure_pwd.value) {
			fire('zjc-000002', 'ec-001', 'fail', '请输入确认密码');
			return;
		}
		if (new_pwd.value !== sure_pwd.value) {
			fire('zjc-000002', 'ec-001', 'fail', '确认密码与新密码不一致');
			return;
		}
		if (new_pwd.value === current_pwd.value) {
			fire('zjc-000002', 'ec-001', 'fail', '新密码与旧密码一致');
			return;
		}
		// const usercode = (await user_info('user')).usercode;

		try {
			// const param = {
			// 	edit_type: 'normal',
			// 	id: usercode,
			// 	password: new_pwd.value,
			// 	captcha: current_pwd.value
			// };

			const user = await whoami(get_user_info) as {
				productid: string;
				usercode: string
			};
			const usercode = user.usercode;
			const productid = user.productid;
			const edit_type = 'normal';
			const id = user.usercode;
			const captcha = current_pwd.value;
			const password = new_pwd.value;

			const res = await update_pwd(url, productid, edit_type, id, password, captcha, false, false)


			if (res['result'] == true) {
				// 修改密码成功
				// 退出系统
				// console.log(usercode, 'sssssssssss')

				await user_loginout(fd.data.spaceid, fd.data.systemid, usercode);
				await storage_clear('global');
				await storage_clear();
				url_redirect('login.html', {});
			} else {
				// 修改密码失败
				fire('zjc-000002', 'ec-001', 'fail', res['msg']);
			}
		} catch (error) {
			url_redirect('login.html', {});
		}
	}
};

const s = {
	'cmpt_show': 'cmpt_show',
	'close_cmpt': 'close_cmpt',
	'upd_pwd': 'upd_pwd'
};

export default function main(url: string, query: any) {
	return init(id, s, actions, url, query, { spaceid, systemid, refsystemid });
}
